package com.naiterui.ehp.bs.doctor.controller;

import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bs.doctor.exception.ExceptionCodes;
import com.naiterui.ehp.bs.doctor.service.IDrAppH5MapperService;
import com.naiterui.ehp.bs.doctor.vo.H5InfoVO;
import com.naiterui.ehp.bs.doctor.vo.param.AppH5ParamVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

/**
 * app和h5版本映射关系接口
 * 当前用于app端从服务器下载对应的h5压缩包进行嵌入
 *
 * @author gongbaoqiang
 * @date 2019/7/15 16:50
 * @since 1.0.0
 */
@Slf4j
@Api(tags = {SwaggerTag.DOCTOR + SwaggerTag.VERSION_CONTROL})
@RequestMapping("ad/app/h5")
@RestController
@AllArgsConstructor
public class AdDrAppH5MapperController {
    private final IDrAppH5MapperService mapperService;

    @ApiOperation(value = "获取app版本对应的h5信息", notes = "获取app版本对应的h5信息", httpMethod = "GET")
    @ApiImplicitParams({@ApiImplicitParam(name = "h5V", value = "h5版本号", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "appV", value = "app版本号", required = true, dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "type", value = "设备类型：1 android, 2 ios", required = true, dataType = "int", paramType = "query")})
    @GetMapping("upgrade")
    public ResponseVO<H5InfoVO> upgrade(@NotEmpty String h5V, @NotEmpty String appV, @NotEmpty Integer type) throws BusinessException {
        log.info("获取app版本对应的h5信息: h5V={}, appV={}, type={}", h5V, appV, type);
        H5InfoVO infoVO = this.mapperService.findH5ZipByAppVerion(h5V, appV, type);
        return PublicService.returnResponseVO(infoVO);
    }

    /**
     * <pre>添加新配置记录</pre>
     *
     * @param paramVO
     *
     * @return
     *
     * @throws BusinessException
     */
    @ApiIgnore
    @PostMapping("newMapper")
    public ResponseVO<Void> newMapper(@Valid @NotNull AppH5ParamVO paramVO) throws BusinessException {
        this.mapperService.add(paramVO);
        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }
}
